package com.biaoyoo.www.personapp.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.biaoyoo.www.personapp.R;
import com.biaoyoo.www.personapp.core.BaseViewHolder;
import com.biaoyoo.www.personapp.widget.DividerItemDecoration;

import java.util.ArrayList;

/**
 * <pre>
 *     author : 40575
 *     e-mail : huyongxi2015@gmail.com
 *     time   : 2017/05/24
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public abstract class BaseListActivity<T> extends BaseActivity {

    protected RecyclerView mRecycleView;
    protected SwipeRefreshLayout mSwipeRefreshLayout;
    protected SimpleListAdapter mAdapter;
    protected ArrayList<T> mDataList ;

    @Override
    protected void loadViews() {
        mRecycleView = (RecyclerView) findViewById(R.id.recycle_view);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    }

    @Override
    protected void initDatas() {

        mRecycleView.setLayoutManager(getLayoutManager());
        mAdapter = new SimpleListAdapter();
        mRecycleView.setAdapter(mAdapter);
        RecyclerView.ItemDecoration decoration = getItemDecoration();
        if(decoration!=null){
            mRecycleView.addItemDecoration(decoration);
        }


    }

    protected RecyclerView.ItemDecoration getItemDecoration(){
        return new DividerItemDecoration(getApplicationContext(),R.drawable.list_item_divider);
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getApplicationContext());
    }


    /**
     * 二次封装，绑定操作可以刚到baseViewhodler中
     */
//    class SimpleListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    class SimpleListAdapter extends RecyclerView.Adapter<BaseViewHolder>{

        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return onCreateHolder(parent,viewType);
        }

        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
            holder.onBind(position);
        }

      /*  @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            onBindHolder(holder,position);
        }*/

        @Override
        public int getItemCount() {
            return mDataList != null ? mDataList.size() : 0;
        }
    }

    protected abstract BaseViewHolder onCreateHolder(ViewGroup parent, int viewType);

}
